package com.ffzx.parentweb.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ffzx.commerce.framework.controller.BaseController;
import com.ffzx.commerce.framework.utils.FreeMarkerUtils;
import com.ffzx.commerce.framework.utils.StackTraceToString;
import com.ffzx.commerce.framework.utils.UUIDGenerator;

/**
 * ExceptionController
 * 
 * @author CMM
 *
 * 2016年6月16日 下午8:34:51
 */
@Controller
public class ExceptionController extends BaseController {
	
	// 记录日志
	private final static Logger logger = LoggerFactory.getLogger(ExceptionController.class);
	
	@RequestMapping(value = "/500")
	public void exception(HttpServletRequest request, HttpServletResponse response) {
		Throwable ex = null;
		if (request.getAttribute("exception") != null) {
			ex = (Throwable) request.getAttribute("exception");
		} else if (request.getAttribute("javax.servlet.error.exception") != null) {
			ex = (Throwable) request.getAttribute("javax.servlet.error.exception");
		}
		
		//错误唯一标识，为了异常日志的跟踪
		String errorCode = UUIDGenerator.getUUID();
		String errorMassage = ex.getMessage();
		String errorStackTrace = StackTraceToString.getStackTrace(ex);
				
		//输出异常信息
		logger.error("errorCode:" + errorCode);
		//logger.error("错误信息:", errorMassage);
		//logger.error("错误堆栈:", errorStackTrace);
		logger.error("错误信息", ex);

		//ajax请求
		if (request.getHeader("accept").indexOf("application/json") > -1 || (request.getHeader("X-Requested-With") != null && request.getHeader("X-Requested-With").indexOf("XMLHttpRequest") > -1)) {
			response.reset();
			response.setContentType("application/json;charset=utf-8");
			
			try {
	        	response.getWriter().print("操作错误！");
	        	response.getWriter().flush();
	        } catch (IllegalStateException ise) {  // If getOutputStream() has already been called
	        	try {
					response.getOutputStream().print("操作错误！");
					response.getOutputStream().flush();
				} catch (IOException ioe) {
					logger.error("异常处理错误(GenericHandlerExceptionResolver):", ioe);
				}
	        } catch (IOException ioe) {
	        	logger.error("异常处理错误(GenericHandlerExceptionResolver):", ioe);
			}
			
		}else{
			Map<String, Object> model = new HashMap<String, Object>();
			model.put("errorCode", errorCode);
			model.put("errorMassage", errorMassage);
			model.put("errorStackTrace", errorStackTrace);

			response.reset();
			response.setContentType("text/html;charset=utf-8");
			try {
				String htmlString = FreeMarkerUtils.process("/error/500.ftl", model);
				response.getWriter().print(htmlString);
	        	response.getWriter().flush();
	        } catch (IllegalStateException ise) {  // If getOutputStream() has already been called
	        	try {
					response.getOutputStream().print("操作错误！");
					response.getOutputStream().flush();
				} catch (IOException ioe) {
					logger.error("异常处理错误(GenericHandlerExceptionResolver):", ioe);
				}
	        } catch (IOException ioe) {
	        	logger.error("异常处理错误(GenericHandlerExceptionResolver):", ioe);
			}catch (Exception e) {
	        	logger.error("异常处理错误(GenericHandlerExceptionResolver):", e);
			}
		}
	}

}
